<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,100分)- 优秀学员统计（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>公司某部门软件教导团正在组织新员工每日打卡学习活动&#xff0c;他们开展这项学习活动已经一个月了&#xff0c;所以想统计下这个月优秀的打卡员工。每个员工会对应一个id&#xff0c;每天的打卡记录记录当天打卡员工的id集合&#xff0c;一共30天。</p> 
<p>请你实现代码帮助统计出打卡次数top5的员工。加入打卡次数相同&#xff0c;将较早参与打卡的员工排在前面&#xff0c;如果开始参与打卡的时间还是一样&#xff0c;将id较小的员工排在前面。</p> 
<p>注&#xff1a;不考虑并列的情况&#xff0c;按规则返回前5名员工的id即可&#xff0c;如果当月打卡的员工少于5个&#xff0c;按规则排序返回所有有打卡记录的员工id。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行输入为新员工数量N&#xff0c;表示新员工编号id为0到N-1&#xff0c;N的范围为[1,100]</p> 
<p>第二行输入为30个整数&#xff0c;表示每天打卡的员工数量&#xff0c;每天至少有1名员工打卡。</p> 
<p>之后30行为每天打卡的员工id集合&#xff0c;id不会重复。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>按顺序输出打卡top5员工的id&#xff0c;用空格隔开。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">11<br /> 4 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2<br /> 0 1 7 10<br /> 0 1 6 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 10<br /> 6 10<br /> 7 10</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">10 0 1 7 6</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">员工编号范围为0~10&#xff0c;id为10的员工连续打卡30天&#xff0c;排第一&#xff0c;id为0,1,6,7的员工打卡都是两天&#xff0c;id为0,1,7的员工在第一天就打卡&#xff0c;比id为6的员工早&#xff0c;排在前面&#xff0c;0,1,7按id升序排列&#xff0c;所以输出[10,0,1,7,6]</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:84px;">输入</td><td style="width:414px;">7<br /> 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5<br /> 0 1 2 3 4 5</td></tr><tr><td style="width:84px;">输出</td><td style="width:414px;">0 1 2 3 4</td></tr><tr><td style="width:84px;">说明</td><td style="width:414px;">员工编号范围为0-6&#xff0c;id为0&#xff0c;1&#xff0c;2&#xff0c;3&#xff0c;4&#xff0c;5的员工打卡次数相同&#xff0c;最早开始打卡的时间也一样&#xff0c;所以按id升序返回前5个id</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:84px;">输入</td><td style="width:414px;">2<br /> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 1<br /> 0 1<br /> 0 1</td></tr><tr><td style="width:84px;">输出</td><td style="width:414px;">1 0</td></tr><tr><td style="width:84px;">说明</td><td style="width:414px;">只有两名员工参与打卡&#xff0c;按规则排序输出两名员工的id</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>简单排序题。需要注意的是&#xff0c;排序要素需要记录每个员工第一次打卡日期&#xff0c;作为第二优先级排序。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 32) {
    const n &#61; lines[0] - 0;
    const dayCount &#61; lines[1].split(&#34; &#34;).map(Number);
    const dayIds &#61; lines.slice(2).map((line) &#61;&gt; line.split(&#34; &#34;).map(Number));
    console.log(getResult(dayIds));

    lines.length &#61; 0;
  }
});

function getResult(dayIds) {
  const employees &#61; {};

  for (let i &#61; 0; i &lt; dayIds.length; i&#43;&#43;) {
    const ids &#61; dayIds[i];
    for (let id of ids) {
      if (employees[id]) {
        employees[id].count&#43;&#43;;
      } else {
        employees[id] &#61; {
          count: 1,
          firstDay: i,
        };
      }
    }
  }

  let arr &#61; [];
  for (let id in employees) {
    const { count, firstDay } &#61; employees[id];
    arr.push([id, count, firstDay]);
  }

  arr.sort((a, b) &#61;&gt;
    b[1] !&#61;&#61; a[1] ? b[1] - a[1] : b[2] !&#61;&#61; a[2] ? a[2] - b[2] : a[0] - b[0]
  );

  return arr
    .slice(0, 5)
    .map(([id]) &#61;&gt; id)
    .join(&#34; &#34;);
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringJoiner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();

    int[] dayCount &#61; new int[30];
    for (int i &#61; 0; i &lt; 30; i&#43;&#43;) {
      dayCount[i] &#61; sc.nextInt();
    }

    int[][] dayIds &#61; new int[30][];
    for (int i &#61; 0; i &lt; 30; i&#43;&#43;) {
      int m &#61; dayCount[i];
      dayIds[i] &#61; new int[m];
      for (int j &#61; 0; j &lt; m; j&#43;&#43;) {
        dayIds[i][j] &#61; sc.nextInt();
      }
    }

    System.out.println(getResult(dayIds));
  }

  public static String getResult(int[][] dayIds) {
    HashMap&lt;Integer, Integer[]&gt; employees &#61; new HashMap&lt;&gt;();

    for (int i &#61; 0; i &lt; dayIds.length; i&#43;&#43;) {
      int[] ids &#61; dayIds[i];

      for (int id : ids) {
        if (employees.containsKey(id)) {
          employees.get(id)[0]&#43;&#43;;
        } else {
          // 加入数组含义是&#xff1a;该id员工的 [打卡次数&#xff0c;第一天打卡日期]
          employees.put(id, new Integer[] {1, i});
        }
      }
    }

    ArrayList&lt;Integer[]&gt; list &#61; new ArrayList&lt;&gt;();
    for (Integer id : employees.keySet()) {
      Integer[] employee &#61; employees.get(id);
      int count &#61; employee[0];
      int firstDay &#61; employee[1];
      list.add(new Integer[] {id, count, firstDay});
    }

    list.sort(
        (a, b) -&gt;
            a[1].equals(b[1]) ? (a[2].equals(b[2]) ? a[0] - b[0] : a[2] - b[2]) : b[1] - a[1]);

    StringJoiner sj &#61; new StringJoiner(&#34; &#34;);
    // 不考虑并列的情况&#xff0c;按规则返回前5名员工的id即可&#xff0c;如果当月打卡的员工少于5个&#xff0c;按规则排序返回所有有打卡记录的员工id
    for (int i &#61; 0; i &lt; Math.min(5, list.size()); i&#43;&#43;) {
      sj.add(list.get(i)[0] &#43; &#34;&#34;);
    }
    return sj.toString();
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
dayCount &#61; list(map(int, input().split()))

dayIds &#61; []
for i in range(30):
    dayIds.append(list(map(int, input().split())))


# 算法入口
def getResult(dayIds):
    employees &#61; {}

    for i in range(len(dayIds)):
        ids &#61; dayIds[i]
        for id in ids:
            if employees.get(id) is not None:
                employees[id][&#34;count&#34;] &#43;&#61; 1
            else:
                employees[id] &#61; {
                    &#39;count&#39;: 1,
                    &#39;firstDay&#39;: i
                }

    arr &#61; []
    for id in employees.keys():
        arr.append((id, employees[id][&#34;count&#34;], employees[id][&#34;firstDay&#34;]))

    arr.sort(key&#61;lambda x: (-x[1], x[2], x[0]))

    return &#34; &#34;.join(list(map(lambda x: str(x[0]), arr[:5])))


print(getResult(dayIds))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>